import { resource } from "../../api";

const MUTATION_TYPES = {
  RESOURCES: "RESOURCES",
  SUBMIT: "SUBMIT"
};

const state = {};
const getters = {};

const mutations = {
  [MUTATION_TYPES.RESOURCES]() {},
  [MUTATION_TYPES.SUBMIT]() {}
};

const actions = {
  async search({ commit }, filter) {
    let response = await resource.searchName(filter?.name);
    if (response.code === 0) {
      let resources = response.data ? [response.data] : [];
      commit(MUTATION_TYPES.RESOURCES, { resources });
      return resources;
    }
  },
  async submit({ commit }, data) {
    let response = await resource.submit(data);
    if (response.code === 0) {
      commit(MUTATION_TYPES.SUBMIT, response.data);
    }
    return response;
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
